﻿using System;
using System.Collections.Generic;
using System.Text;

/// <summary>
/// Clase utilitaria para crear sentencia select de sql
/// </summary>
public class SelectSentenceBuilder
{
    private string _tableName = null;
    private List<WhereFilter> _filters;

    public List<WhereFilter> Filters
    {
       
        set { _filters = value; }
    }
    private List<string> _columns;

    private string builtSelect = null;


    public SelectSentenceBuilder(string tableName)
    {
        this._tableName = tableName;
        _columns = new List<string>();
        _filters = new List<WhereFilter>();
        _columns.Add("*");
    }
    public void AddColumn(string columnName)
    {
        if (_columns.Count == 1 && _columns[0] == "*" && columnName != "*")
        {
            _columns.Clear();
            _columns.Add(columnName);
        }
        else
            _columns.Add(columnName);
    }

    public void AddAndFilter(WhereFilter filter)
    {
        _filters.Add(filter);
    }
    private string BuildFields()
    {
        string columnsNames = "";
        for (int i = 0; i < _columns.Count; i++)
        {
            if (i == _columns.Count - 1) columnsNames += _columns[i];
            else columnsNames += _columns[i] +",";

        }
        return columnsNames;
    }
    private string BuildWhere()
    {
        if (_filters.Count == 0) return "";




        string filter = "";
        string where = " WHERE ";
        for (int i = 0; i < _filters.Count; i++)
        {
            
                if (i == _filters.Count - 1)
                    filter += _filters[i].ColumnName + FilterOpToString(_filters[i].SqlOperator) + FilterValueToString(_filters[i].SqlOperator, _filters[i].SqlType, _filters[i].ColumnValue);
                else filter += _filters[i].ColumnName + FilterOpToString(_filters[i].SqlOperator) + FilterValueToString(_filters[i].SqlOperator,_filters[i].SqlType, _filters[i].ColumnValue) + " AND ";
            
        }
      

        return where + filter;
    }
    public void Build()
    {
        string select = "SELECT ";
        string fields = BuildFields();
        string where = BuildWhere();

        builtSelect = select + fields + " FROM " + _tableName + where;
    }

    public string GetBuiltSelect()
    {
        return builtSelect;
    }
    private string FilterOpToString(WhereFilter.WhereOperation compaOpe)
    {
        switch (compaOpe)
        {
            case WhereFilter.WhereOperation.Equal: return " = ";
            case WhereFilter.WhereOperation.LessThan: return " < ";
            case WhereFilter.WhereOperation.MoreThan: return " > ";
            case WhereFilter.WhereOperation.Like: return " like ";
            case WhereFilter.WhereOperation.MoreEqualThan: return ">=";
            case WhereFilter.WhereOperation.LessEqualThan: return "<=";
            default: throw new ApplicationException("Not yet suported");
        }
    }
    private string FilterValueToString(WhereFilter.WhereOperation operation ,WhereFilter.SQLValueType type,string value )
    {
        if (operation==WhereFilter.WhereOperation.Like)
        {
            return "'" + value + "%'";
        }
        if (type == WhereFilter.SQLValueType.String)
        {
            return "'" + value + "'";
        }
        else return value;
    }









































































}
